package com.powerManager.sdk.handlers;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ColumnMapRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;

import com.powerManager.sdk.common.InitSystem;
import com.powerManager.sdk.common.JdbcSingleton;
import com.powerManager.sdk.common.PagingResult;

public class ButtonHandler {

	public String buttonTreeJSON(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String sql="select MenuName as text,MenuNo as id,MenuNo,MenuParentNo from Sys_Menu where MenuParentNo ='0'";
		PagingResult pr = new PagingResult(sql);
		String json = null;
		try {
			json = pr.treeToJsonBymodule(sql, null, "MenuNo", "MenuParentNo", "0");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//[{"+"text"+":"+"主菜单"+json+"}]"
		//json="[{\"text\":\"主菜单\",\"children\":[{\"text\":\"基础信息管理\"}," +
		//		"{\"text\":\"客户管理\"}," +
		//		"{\"text\":\"组织架构\"}," +
		//		"{\"text\":\"系统管理\"}]}]";
		json="[{\"text\":\"主菜单\",\"MenuNo\":\"0\",\"children\":"+json+"}]";
		System.out.println("text:"+json);
		return "text:"+json;
	}
	public String ButtonGirdJSON(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String sql="select BtnNo,BtnName,BtnIcon from Sys_Button ";
		String where = request.getParameter("where");
		
		if(!(where.isEmpty())){
			sql=sql+" where MenuNo ='"+where+"'";
		}
		System.out.println(sql);
		PagingResult pr = new PagingResult(sql);
		String json = null;
		try {
			json = pr.pagingToJson(sql, null, 30, 1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//[{"+"text"+":"+"主菜单"+json+"}]"
		//json="[{\"text\":\"主菜单\",\"children\":[{\"text\":\"基础信息管理\"}," +
		//		"{\"text\":\"客户管理\"}," +
		//		"{\"text\":\"组织架构\"}," +
		//		"{\"text\":\"系统管理\"}]}]";
		//json="[{\"text\":\"主菜单\",\"children\":"+json+"}]";
		System.out.println("text:"+json);
		return "text:"+json;
	}
	
	
	/** 
	* <p>Title: addRole</p> 
	* <p>Description:新增菜单</p> 
	* @param request
	* @param response
	* @return 
	*/ 
	@SuppressWarnings("static-access")
	public boolean addButton(HttpServletRequest request, HttpServletResponse response){
		JdbcSingleton jdbc = new JdbcSingleton();
		//描述
		String MenuNo = request.getParameter("MenuNo");
		
		String sql="INSERT INTO sys_button (BtnName, BtnNo, BtnIcon, MenuNo) VALUES (?, ?, ?, ?)";
		Object[] params = new Object[]{MenuNo};
		
		int result = jdbc.getJdbcTemplate().update(sql, params);
		InitSystem.loadButton();
		return result>0?true:false;
	}
	
	
	/** 
	* <p>Title: updateRole</p> 
	* <p>Description: 更改菜单信息</p> 
	* @param request
	* @param response
	* @return 
	*/ 
	@SuppressWarnings("static-access")
	public boolean updateButton(HttpServletRequest request, HttpServletResponse response){
		JdbcSingleton jdbc = new JdbcSingleton();
	String MenuNo = request.getParameter("MenuNo");
		
		String sql="UPDATE sys_button SET BtnName=?,BtnNo=?,BtnIcon=?, MenuNo=? WHERE BtnID=?";
		Object[] params = new Object[]{MenuNo};
		
		int result = jdbc.getJdbcTemplate().update(sql, params);
		InitSystem.loadButton();
		return result>0?true:false;
	}
	
	/** 
	* <p>Title: deleteRole</p> 
	* <p>Description: 删除菜单信息</p> 
	* @param request
	* @param response
	* @return 
	*/ 
	@SuppressWarnings("static-access")
	public boolean deleteButton(HttpServletRequest request, HttpServletResponse response){
		JdbcSingleton jdbc = new JdbcSingleton();
		//获取角色Id
		String roleId = request.getParameter("RoleId");
		String sql = "delete from sys_role where role_id=?";
		Object[] params = new Object[]{roleId};
		
		int result = jdbc.getJdbcTemplate().update(sql, params);
		InitSystem.loadButton();
		return result>0?true:false;
	}	
}
